//
//  ChangeAccountViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/20.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ChangeAccountViewController.h"
#import "UserInfoCell.h"
#import "SystemSetingCell.h"

@interface ChangeAccountViewController ()

@property (nonatomic, strong) LoginAccountModel *model;

@property (nonatomic, strong) LoginItemModel *aimsItemModel;


@end

@implementation ChangeAccountViewController

#pragma mark - view life cycle

- (void)setupUI{
    
    self.baseNavView.navTitleLabel.text = @"切换账号";
    self.aimsItemModel = nil;
    self.tableView.rowHeight = 54.f;
    self.tableView.separatorColor = mIconLineColor;
}

- (void)setupLayout{
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.right.left.bottom.mas_equalTo(self.view);
    }];
}


//获取帐号切换列表
- (void)initData{
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] getLoginAccountList] subscribeNext:^(LoginAccountModel * _Nullable model) {
        @strongify(self);
        self.model = model;
        [self.tableView reloadData];
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self showHudWithMsg:@"由于网络问题，请稍候再试！"];
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.list.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return  [self addTableViewSectionSegmentationLineByY:9.5];
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return  [self addTableViewSectionSegmentationLineByY:0.0];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChangeAccountCell *cell = [ChangeAccountCell cellWithTableView:tableView];
    LoginItemModel *itemModel = self.model.list[indexPath.row];
    cell.itemModel = itemModel;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LoginItemModel *itemModel = self.model.list[indexPath.row];
    itemModel.selected = YES;

    if (!self.aimsItemModel) {
        self.aimsItemModel = itemModel;
    }else{
        self.aimsItemModel.selected = NO;
        self.aimsItemModel = itemModel;
    }
    [self.tableView reloadData];
}


@end
